/**
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

#import "AppDelegate.h"

#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>

#import <AlipaySDK/AlipaySDK.h>
#import "WXApi.h"

#import "ActivityPageOne.h"

@interface AppDelegate() <WXApiDelegate>
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//  NSURL *jsCodeLocation;
//
//  #ifdef DEBUG
//    jsCodeLocation = [NSURL URLWithString:@"http://192.168.121.146:8081/index.ios.bundle?platform=ios"];
//  #else
//   // jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"index.ios" withExtension:@"jsbundle"];
//   jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];
//  #endif
//
//
//
//  RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
//                                                      moduleName:@"eCourseApp"
//                                               initialProperties:nil
//                                                   launchOptions:launchOptions];
//  rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];
//  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
//  UIViewController *rootViewController = [UIViewController new];
//  rootViewController.view = rootView;
//  self.window.rootViewController = rootViewController;
//  [self.window makeKeyAndVisible];
  //  -----------
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  
  UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[ActivityPageOne new]];
  self.window.rootViewController = nav;
  [self.window makeKeyAndVisible];
  
  return YES;
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
{
  if ([url.host isEqualToString:@"safepay"]) { // 支付宝
    // 支付跳转支付宝钱包进行支付，处理支付结果
    [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
      NSLog(@"已授权 result = %@", resultDic);
    }];
    
    // 授权跳转支付宝钱包进行支付，处理支付结果
    [[AlipaySDK defaultService] processAuth_V2Result:url standbyCallback:^(NSDictionary *resultDic) {
      NSLog(@"授权 result = %@", resultDic);
      // 解析 auth code
      NSString *result = resultDic[@"result"];
      NSString *authCode = nil;
      if (result.length>0) {
        NSArray *resultArr = [result componentsSeparatedByString:@"&"];
        for (NSString *subResult in resultArr) {
          if (subResult.length > 10 && [subResult hasPrefix:@"auth_code="]) {
            authCode = [subResult substringFromIndex:10];
            break;
          }
        }
      }
      NSLog(@"授权结果 authCode = %@", authCode?:@"");
    }];
    return YES;
  }
  else { // 微信支付
    return [WXApi handleOpenURL:url delegate:self];
  }
}

#pragma mark - WXApiDelegate
- (void)onResp:(BaseResp *)resp {
  // 判断是否是微信支付回调 (注意是PayResp 而不是PayReq)
  if ([resp isKindOfClass:[PayResp class]]) {
    //发出通知 从微信回调回来之后,发一个通知,让请求支付的页面接收消息,并且展示出来,或者进行一些自定义的展示或者跳转
    NSNotification *notification = [NSNotification notificationWithName:@"WeChatPay" object:nil userInfo:@{@"errCode":@(resp.errCode)}];
    [[NSNotificationCenter defaultCenter] postNotification:notification];
  }
}

@end
